const BaseRest = require('../rest.js');
const Tools = require('../../tools/tools');
module.exports = class extends BaseRest
{

    constructor(ctx)
    {
        super(ctx);
        this._CPInstance = this.model('apiv1/competition');
        this._service = this.service('apiv1/competition');
        this.errNo = 0;
        this.errMsg = 'ok';
    }

    /**
     * 获取所有比赛&&获取指定比赛
     * @returns {Promise.<*>}
     */
    async getAction()
    {
        if (this._guid)
        {
            return this.success(await this._getCPByGuid());
        } else
        {
            return this.success(await this._getCP());
        }

    }

    /**
     * 添加比赛
     * @return {Promise} []
     */
    async postAction()
    {

        let post = this.post('data');
        let postData = Tools.JQueryPostJsonToObj(this.ctx);
        if (!think.isObject(postData))
        {
            this.errNo = 52001;
            this.errMsg = '提交数据错误,或未提交数据,参考52004';
            this.ctx.fail(this.errNo, this.errMsg);
            return false;
        }

        let postDataStat = this._service.createCompetition(postData);

        if (postDataStat.isErr === false)
        {
            //获取提交的数据成功
            //存储数据库
            //todo 存储数据到数据库
            let dbRes = await this._service.saveData();
            if (dbRes.isErr !== true)
            {
                this.ctx.fail(dbRes.errNo, dbRes.errMsg);
                return false;
            } else
            {
                this.ctx.fail(dbRes.errNo, dbRes.errMsg);
                return true;
            }
        } else
        {
            //获取数据失败
            return this.ctx.fail(postDataStat.errNo, postDataStat.errMsg);

        }

    }

    /**
     * delete resource
     * @return {Promise} []
     */
    async deleteAction()
    {
        if (!this._guid)
        {
            return this.error(52015, '删除数据时guid不能为空,请检查参数')
        }
        let res = await this._CPInstance.where({'PK_CompetitionGuid': this._guid}).update({'IsDelete': "已删除"});

        if (res === 1)
        {
            return this.error(0, "删除成功");
        } else
        {
            return this.error(52016, "该比赛不存在,请检查guid");
        }
    }

    /**
     * update resource
     * @return {Promise} []
     */
    async putAction()
    {
        return this.success('.....');
        // if (!this._guid) {
        //     return this.fail('params error');
        // }
        // const pk = this.modelInstance.pk;
        // const data = this.post();
        // delete data[pk];
        // if (think.isEmpty(data)) {
        //     return this.fail('data is empty');
        // }
        // const rows = await this.modelInstance.where({ [pk]: this._guid }).update(data);
        // return this.success({ affectedRows: rows });
    }

    /**
     * 获取指定比赛的作品数量
     * @returns {Promise.<*>}
     */
    async competitionWorksCountAction()
    {
        return this.success(await this._getCPWorksCountByWorksGuid());
    }

    /**
     * 比赛介绍
     * @returns {Promise.<void>}
     */
    async competitionDescAction(){
        let data=await  this._CPInstance.fieldReverse('FK_TeacherGuid,IsDelete').find(this._guid);
        return this.end(data);
    }

    /**
     * 比赛流程
     * @returns {Promise.<*>}
     */
    async competitionStepAction(){
        let data=await this.model('apiv1/competitionSteps').where({FK_CompetitionGuid:this._guid}).select();
        return this.end(data);
    }

    /**
     * 获取全部比赛
     * @returns {Promise.<{nowCompetition: {total: *, items: *}, oldCompetition: {total: *, items: *}}>}
     * @private
     */
    async _getCP()
    {
        this.finishedTotal = await this._CPInstance.getFinishedTotal();
        this.nowItems = await this._CPInstance.getNowItems();
        this.finishedItems = await  this._CPInstance.getFinishedItems();
        this.nowTotal = await this._CPInstance.getNowTotal();
        return {
            'nowCompetition': {
                'total': this.nowTotal,
                'items': this.nowItems
            },
            'oldCompetition': {
                'total': this.finishedTotal,
                'items': this.finishedItems
            }
        };
    }

    /**
     * 获取指定作品数量
     * @returns {Promise.<{worksTotal: *}>}
     * @private
     */
    async _getCPWorksCountByWorksGuid()
    {
        let count = await think.service('apiv1/competition').getCPWorksCountByWorksGuid(this._guid);
        let res = count ? count : 0;
        return {
            'worksTotal': count
        }
    }

    /**
     * 根据guid获取比赛信息
     * @returns {Promise.<void>}
     * @private
     */
    async _getCPByGuid()
    {
        return await this._CPInstance.getCPByGuid(this._guid);
    }




};
